exports.GameCentralHandler = Core.Class.singleton({
    classname: 'GameCentralHandler',
    
    initialize: function() 
    {
    	//NgLogE('Game Central Handler Log');
		this._splashScreen = null;
		this._dbSyncHandler = null;
		this._storeIdsArray = null;
		this._orientationInterfaceListener = null;
		
		this.initiateSingletons();
		this._loadingScreen = new GameLoadingScreen();
//		this.gameLoader = new GameLoader();
    },

	initiateSingletons: function()
	{
		//console.log("Zulu: Singletons initiated");
		
		MMStorageManager.instantiate();
//		TJAudioManager.setBGMusicPath(BACKGROUND_MUSIC_PATH);
//		TJAudioManager.playBgMusic();
//		TJAudioManager.initializeSoundEffect();

		MMGameState.instantiate();
		
		MMGameLayer.instantiate();
		AnalyticsHandler.instantiate();
		
		DatabaseHelper.instantiate();
		console.log("********************device language is ********************"+Core.Capabilities.getLanguage());
		///************ InApp Test Lines *********///
		var enforceLanguage = 'cn';
		GALocalizationHandler.setGameLanguage(enforceLanguage);
	},

	loadSuccessForStorageManager: function() {
		//console.log("Zulu: loadSuccessForStorageManager");
		
//		var key = "NotFirstRun";
//		var notFirstRun = MMStorageManager.getValueFromUserDefault(key);
//		if(!notFirstRun){
//			this._loadingScreen.setLoadingImage();
//		}

//		this._dbSyncHandler = new DBSyncHandler();
//		this._dbSyncHandler.loadExistingDBResource(MM_CURRENT_DB_PATH, MM_DB_RESOURCES, this.loadDBResourceSuccess.bind(this));
		this.loadDBResourceSuccess();
		MMSoundManager.instantiate();
	},

	loadDBResourceSuccess: function()
	{
		var url = Core.Capabilities.getContentUrl();
//		GAInAppPurchaseHandler.instantiate();
		//console.log("Zulu: loadSecondry Menifest ");										  
		this._downloader = new ManifestContentDownloader(url, "./", "webgame.00.ngmanifest", this);
		
		var myString = TCUtil.getRelativeString(Core.Localization.getString("DOWNLOADING_RESOURCES"), ["0"]);
		this._loadingScreen.showProgressLabel(myString);
		
	},

	updateMenifestProgress: function(percentage) {
		var myString = TCUtil.getRelativeString(Core.Localization.getString("DOWNLOADING_RESOURCES"), [percentage]);
		this._loadingScreen.updateProgressLabel(myString);
	},

	doneDownloading: function() {
		// This is MenifestContentDownloader Callback
		//console.log("Zulu: doneDownloading");										  
		this._loadingScreen.hideProgressLabel();
//		if(this._dbSyncHandler) {
//			this._dbSyncHandler.startSynchingUserData(this.dbSyncHandlerSuccess.bind(this));
//		} else {
			this.dbSyncHandlerSuccess();
//		}
		
	},

	dbSyncHandlerSuccess: function()
	{
		var connectHandler = new GAPIConnectHandler();
		MMFriendManager.instantiate();
	},

	onConnectSuccess: function()
	{
		DatabaseInterface.startFetchingUserData(this.loadSuccessForDatabaseInterface.bind(this));
		//this.loadSuccessForDatabaseInterface();
	},

	loadSuccessForDatabaseInterface :function()
	{
		
		MMGameState.initiateUser();
        										  
		/*MMGameState._serverTimeDiff = GAGameLoader.getResponseObject().getTimeDifference();
		 MMGameState.initiateUser();
		 this._storeIdsArray = MMGameState.getCurrentUser().getSelectedZoo().getAllStoreIds();	
		 this._pvrStoreIds = MMGameState.getCurrentUser().getSelectedZoo().getAllPvrStoreIds();//""
		 //NgLogD("storeIdsArray "+this._storeIdsArray);												  
		 this._resourceSyncHandlerObj = new GAResourceSyncHandler(this._storeIdsArray, this.resourceSyncHandlerSuccessful.bind(this), this.resourceSyncHandlerFailed.bind(this), this.resourcesSyncHandlerProgress.bind(this),this._pvrStoreIds);
		 this._resourceSyncHandlerObj.startLoading();
		 */
		
		this.resourceSyncHandlerSuccessful();
		
		//var myString = TCUtil.getRelativeString(Core.Localization.getString("DOWNLOADING_RESOURCES"), ["0"]);
		//this._loadingScreen.showProgressLabel(myString);		
		
	},

	resourceSyncHandlerSuccessful:function () 
	{											
		this._loadingScreen.hideProgressHUD();
		if(this._resourceSyncHandlerObj) 
		{
			this._resourceSyncHandlerObj.destroy();
			this._resourceSyncHandlerObj = null;
		}
		
		//this._loadingScreen.updateProgress(5);//Will Update Creating UI Label
		
		this.startGame();
	},

	startGame: function()
	{
		if(!DatabaseInterface._isVisitingNeighbor)
		{
			TutorialManager.instantiate();
			AnalyticsHandler.gameConnect();
			MMFriendActionManager.instantiate();
		}
		MMGameLayer.showMallListUI();
		this.destroyTransitionScreen();
	},
    
    getGameLoader: function()
    {
    	return this.gameLoader;
    },
    
	addUITransitionScreen: function()
	{
		
		UIFactory.showBlockView();
		var w = TCUtil.getAdjustedX(320);
		var h = TCUtil.getAdjustedY(480);
		
		this._uiTransitionScreen = UIFactory.getImage([0,0,w, h], './Content/Default.png');								
		NGWindow.document.addChild(this._uiTransitionScreen);
		
	},
	
	removeUITransitionScreen: function()
	{
		if(this._uiTransitionScreen)
		{
			this._uiTransitionScreen.destroy();
			this._uiTransitionScreen = null;
		}
	},
	
	addTransitionScreen: function()
	{
		UIFactory.showBlockView();
		this._loadingScreen = new GameLoadingScreen();
		this._loadingScreen.startLoadingScreen();
		if(DatabaseInterface._isVisitingNeighbor)
		{
			GAMobageSocialHandler.hideCommunityButton();
//			var textMessage = null;
//			if(DatabaseHelper.getIsFriend())
//			{
//				textMessage = Core.Localization.getString("LOADING_FRIEND");
//			}
//			else
//			{
//				textMessage = Core.Localization.getString("LOADING_NEIGHBOUR");
//			}
			
//			this._loadingScreen._loadingText.setText(Loading);//"Loading Neighbor");
		}
		else
		{
			//this._loadingScreen._loadingText.setText(Core.Localization.getString("LOADING"));
		}
		
	},
	
    loadSplashScreen: function()
    {
    	this._splashScreen = new SplashScreen();
		this._splashScreen.startSplashScreen();
												  
    },
    
	startLoadingScreen:function ()
	{
		this._loadingScreen.startLoadingScreen();
		this._loadingScreen.updateProgress(0);
		MMStorageManager.getAllValuesFromStorage();									  
	},
	
	destroyGameLayer: function()
	{
		UIFactory.showProgressHUD(Core.Localization.getString("LOADING_GAME"));										  
		MMGameLayer.destroyAllChildren();
	}, 
	
	creatGameLayer: function()
	{
		//TZGameLayer.createGameState();
		UIFactory.hideProgressHUD();										  
	},
    
	destroyTransitionScreen: function()
	{
		UIFactory.hideBlockView();

		this._canPerformSusspendAndResumeOperation = true;
		if(this._loadingScreen && Core.ObjectRegistry.isObjectRegistered(this._loadingScreen))
		{
			this._loadingScreen.destroy();
			this._loadingScreen = null;
		}
	},
    
	resourcesSyncHandlerProgress: function(percentage)
	{
		var myString = TCUtil.getRelativeString(Core.Localization.getString("DOWNLOADING_RESOURCES"), [percentage]);
		this._loadingScreen.updateProgressLabel(myString);
	},

	showProgress :function(progressStatus) {
		//console.log("Zeeshan: progress status"+ progressStatus+1);	
		this._loadingScreen.updateProgress(progressStatus+1);
	},										  
			
	clickedButtonAtIndex: function(buttonIndex,alerWindow)
	{
		
		if (buttonIndex == 0) 
		{
			if(alerWindow.getTagValue() == 1111)
			{
				this._resourceSyncHandlerObj.retrySyncHandler();
				
			}	
		} 
		else if (buttonIndex == 1)
		{
			//NgLogE("Cancel button clicked");
		}
		
	},
	setOrientationInterfaceListener :function(listener)
	{
		this._orientationInterfaceListener = listener;
	},
	destroy : function() {
		this._downloader.destroy();
		this._downloader = null;
		this._orientationInterfaceListener.destroy();
		this._orientationInterfaceListener = null;
	} 
});